package com.media.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.media.mapper.CommentMapper;
import com.media.model.Comment;
import com.base.model.R;
import com.media.model.dto.CommentDto;
import com.media.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * @Author: Lenovo
 * @Description: TODO
 * @DateTime: 2023/9/14 15:09
 **/
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private RedisTemplate redisTemplate;

    private static final String COMMENT_PRE = "comment_";

    private static final String USER_PRE = "user_";

    @Autowired
    private CommentMapper commentMapper;

    /**
     * 视频评论
     *
     * @param comment 评论
     * @return 返回信息
     */
    @Override
    public R<String> mediaComment(Comment comment) {
        boolean save = this.save(comment);
        if (!save) {
            return R.success("评论失败，请重新检查评论内容");
        }
        return R.success("评论成功");
    }

    @Override
    public R<List<CommentDto>> listComment(String mediaId) {
        //获取用户Id，暂时先写死，后面去SpringSecurity中获取
        int userId = 1;
        List<CommentDto> commentDtos = commentMapper.listCommentByMediaId(mediaId);
        for (int i=0;i<commentDtos.size();i++) {
            CommentDto commentDto = commentDtos.get(i);
            commentDto.setLikesAmount(getLikeAmountByKey(COMMENT_PRE + commentDto.getId()));
            if (commentDto.getComments() != null) {
                for (int j=0;j<commentDto.getComments().size();j++) {
                    Comment comment=commentDto.getComments().get(j);
                    comment.setLikesAmount(getLikeAmountByKey(COMMENT_PRE + comment.getId()));
                }
            }
        }

        if (redisTemplate.hasKey(USER_PRE + userId)) {
            Set set = (Set) redisTemplate.opsForValue().get(USER_PRE + userId);
            for (int i=0;i<commentDtos.size();i++) {
                CommentDto commentDto=commentDtos.get(i);
                commentDto.setLiked(judgeLikedByKey(set, commentDto.getId()));
                if (commentDto.getComments() != null) {
                    for (int j=0;j<commentDto.getComments().size();j++) {
                        Comment comment=commentDto.getComments().get(j);
                        comment.setLiked(judgeLikedByKey(set, comment.getId()));
                    }
                }
            }
        }
        return R.success(commentDtos);
    }


    private Integer getLikeAmountByKey(String key) {
        if (redisTemplate.hasKey(key)) {
            return (Integer) redisTemplate.opsForValue().get(key);
        } else {
            return 0;
        }
    }

    private Integer judgeLikedByKey(Set set, Integer key) {
        if (set.contains(key)) {
            return 1;
        } else {
            return 0;
        }
    }
}
